﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Adams.Admin.Business
{
    public static class Validation
    {
        public static Adams.Admin.DataTransfer.FormOutput PerformBasicValidation(object input)
        {
            var ret = new Adams.Admin.DataTransfer.FormOutput();

            foreach (PropertyInfo propInfo in input.GetType().GetProperties())
            {
                foreach (ValidationAttribute attr in propInfo.GetCustomAttributes(typeof(ValidationAttribute), true))
                {
                    if (!attr.IsValid(propInfo.GetValue(input, null)))
                    {
                        ret.InvalidFields.Add(new DataTransfer.InvalidField() {
                            Name = propInfo.Name,
                            Code = string.IsNullOrEmpty(attr.ErrorMessage) ? 1 : attr.ErrorMessage.ToInt()
                        });
                    }
                }
            }

            return ret;
        }
    }
}
